# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time

from jms.dwd.dwd_route_basic_detail_dt_input import jms_dwd__dwd_route_basic_detail_dt_input
from utils.alerts.doris_telnet_check import net_is_used
from utils.operators.rich_sql_sensor import RichSqlSensor

label = f"dwd_route_basic_detail_dt_input{int(time())}"
timeout = timedelta(minutes=30).seconds

dorisConn = net_is_used()
doris_jms_dwd__dwd_route_basic_detail_dt_input = RichSqlSensor(
    task_id='doris_jms_dwd__dwd_route_basic_detail_dt_input',
    email=['wangmenglei@jtexpress.com','yl_bigdata@yl-scm.com'],
    pool='broker_load_pool',
    priority_weight=5,
    conn_id='doris',
    pre_sql=f"""
             DELETE FROM jms_dwd.dwd_route_basic_detail where input_date = "{{{{ execution_date | date_add(-90) | cst_ds }}}}";
             LOAD LABEL jms_dwd.{label} (
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dwd.db/external/dwd_route_basic_detail_dt/dt={{{{ execution_date | cst_ds }}}}/is_update=1/*")
                 INTO TABLE dwd_route_basic_detail
                 FORMAT AS 'PARQUET'
             )
             WITH BROKER '{{{{var.json.doris_brokers | random_choice}}}}'
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dwd WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dwd WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120),
    sla=timedelta(hours=6),
)

doris_jms_dwd__dwd_route_basic_detail_dt_input << jms_dwd__dwd_route_basic_detail_dt_input
